package dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;


import domena.Lektor;

@Repository
public class LektorDAOImpl implements LektorDAO {

	@Autowired
	private HibernateTemplate hibernateTemplate;

	@Override
	public void zapiszLektora(Lektor lektor) {
		hibernateTemplate.save(lektor);
	}
	
	@Override
	public void edytujLektora(Lektor lektor){
		hibernateTemplate.update(lektor);
	}
	
	@Override
	public void usunLektora(long id){
		Lektor lektor = getId(id);
		
		if(lektor!=null)
			hibernateTemplate.delete(lektor);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Lektor getId(long id) {
		DetachedCriteria cr = DetachedCriteria.forClass(Lektor.class).add(Restrictions.idEq(id));				
		List<Lektor> list = hibernateTemplate.findByCriteria(cr);
		if(!list.isEmpty())
			return list.get(0);
		return null;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Lektor> listLektor() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Lektor.class);			
		List<Lektor> list = hibernateTemplate.findByCriteria(criteria);
		
		return list;
	}

}